* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file performs propensity score matching between the actual and potential synthetic mergers for regressions analysis of domestic market outcomes.
* We perform nearest neighboring matching outside the 2-digit industry but within the same broad sector group, keeping only the observations that are in the region of common support.
* We also verify that the balancing property holds.

* Setup
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\pscore_pair_vars_02102023.dta", clear
rename aar aarA

merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_dom_all_cvrnr_dro.dta", keepusing(nprod_dro vrdA_dom_dro) keep(match master) nogen
rename nprod_dro nprodA
rename vrdA_dom_dro vrdA

rename cvrnrA cvrnrA_ori
rename cvrnrT cvrnrA
merge m:1 cvrnrA aarA using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_dom_all_cvrnr_dro.dta", keepusing(nprod_dro vrdA_dom_dro) keep(match master) nogen
rename nprod_dro nprodT
rename vrdA_dom_dro vrdT

rename cvrnrA cvrnrT
rename cvrnrA_ori cvrnrA

gen lnvrdA = ln(vrdA)
gen lnvrdT = ln(vrdT)
gen lnnprodA = ln(nprod_before)
gen lnnprodT = ln(nprodT_before)

gen multiA = (lestablishmentA>0)
gen multiT = (lestablishmentT>0)
replace multiA = . if lestablishmentA==.
replace multiT = . if lestablishmentT==.

drop if merger==1 & aar<1999

* Estimate propensity scores
probit merger multiA multiT c.lemployee_cvrnrA c.lemployee_cvrnrT c.lproductivityA c.lproductivityT c.lnnprodA c.lnnprodT c.lOMS_FIRMA c.lOMS_FIRMT overlap nsp i.aarA i.div if lnnprodA!=. & lnnprodT!=.

predict pr

sum pr
local cal = 0.025

keep if e(sample)==1

gen rmerger = 1 - merger
sort div rmerger cvrnrA aarA
by div: gen g = _n
replace g = . if merger==0

egen maxg = max(g), by(div)
expand maxg

* Keep only if outside 2-digit industry code but within same broad sector code
	bysort cvrnrA aarA cvrnrT: gen dup = _n
	drop if dup>1 & merger==1
	
	replace dup = g if merger==1
	egen g2 = group(div dup)
	replace dig4 = floor(dig4/100)
	gen dig4m = dig4 if merger==1
	egen mdig4m = mean(dig4m), by(g2)
	gen aarAm = aarA if merger==1
	egen maarAm = mean(aarAm), by(g2)
	drop if dig4==mdig4m & merger==0
	drop if aarA!=maarAm & merger==0

	gen double pr2 = g2*10 + pr
	
	gen double _x = pr2 if merger==1
	egen double meanpr2 = mean(_x), by(g2)
	egen double maxpr2 = max(pr2) if merger==0, by(g2)
	egen double mmaxpr2 = mean(maxpr2), by(g2)
	egen double minpr2 = min(pr2) if merger==0, by(g2)
	egen double mminpr2 = mean(minpr2), by(g2)	
	
	gen double diff = abs(pr2 - meanpr2) if merger!=1

	local numbermax 2

* Perform propensity score matching		
	while `numbermax'>1 {	
		set seed 12345
		gen s = runiform()
		sort s		
		psmatch2 merger, pscore(pr2) neighbor(1) common caliper(`cal') noreplace		
		gen offsupport = 1 if _support==0 & merger==1
		egen moffsupport = mean(offsupport), by(g2)
		drop if moffsupport==1		
		gen matched = (_weight!=. & merger==0)
		replace matched = . if merger==1
		sort cvrnrA matched aarA cvrnrT dup
		by cvrnrA matched: gen number_matched = _n
		sum number_matched if matched==1
		local numbermax = r(max)
		egen maxnumber_matched = max(number_matched) if matched==1, by(cvrnrA)
		egen mmaxnumber_matched = mean(maxnumber_matched), by(cvrnrA)
		drop if merger==0 & mmaxnumber_matched>1 & mmaxnumber_matched!=. & number_matched!=1
		drop s offsupport moffsupport matched number_matched maxnumber_matched mmaxnumber_matched
	}
	keep if _weight!=.	
	bysort g2: gen temp = _N
	keep if temp==2
	
foreach x in lemployee_cvrnr lestablishment lproductivity lOMS_FIRM{
	gen `x'AXT = `x'A*`x'T
	}

* Perform balancing tests		
foreach x in multiA multiT lemployee_cvrnrA lemployee_cvrnrT lproductivityA lproductivityT lnnprodA lnnprodT lOMS_FIRMA lOMS_FIRMT overlap nsp {
	disp "`x'"
	ttest `x', by(merger)
	}
	
rename aarA aar	

drop nprodA nprodT

compress
save "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_dom_mergers_synthetic_02102023_v4c.dta", replace
